<?php

require_once ('Abstract.php');

class Joob_View_Ajax extends Joob_View_Abstract {
	
	protected $response;
	
	/**
	 * Trap method. Change the specific properties of the AJAX response
	 */
	public function __set($key, $value)	{
		if (isset($this->response))	{
			$this->response = new stdClass();
		}
		$this->response->$key = $value;
	}
	
	public function __get($key)	{
		if (isset($this->response))	{
			return $this->response->$key;
		}
		return null;
	}
	
	public function setAjaxResponse($response)	{
		$this->response = $response;
	}
	
	public function getAjaxResponse()	{
		return $this->response;
	}
	
	public function renderAjax()	{
		return $this->render(null);
	}
	
	/**
	 * Encode the response in JSON format and send it regardless of the script
	 * @see Joob_View_Abstract::run()
	 */
	protected function run() {
		echo json_encode($this->response);
	}
}

?>